/**
 * SystemAction.java
 * Created at 2018-1-3
 * Created by zhanghailin
 * Copyright (C) 2018 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.app.system.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.utils.MD5Util;
import com.broadtext.message.service.IMessageService;
import com.broadtext.rms.session.LoginResult;
import com.broadtext.rms.system.model.VSysUsers;
import com.broadtext.rms.system.service.ILoginService;

/**
 * <p>ClassName: SystemAction</p>
 * <p>Description: TODO</p>
 * <p>Author: zhanghailin</p>
 * <p>Date: 2018-1-3</p>
 */
@Controller
@RequestMapping("/app/system")
public class SystemAction {
    
    
    @Autowired
    @Qualifier("messageService")
    private IMessageService messageService;
    
    /**
     * 登录服务
     */
    @Autowired
    @Qualifier("loginService")
    private ILoginService loginService;
    
    
    @RequestMapping(value = "/login.mvc")
    @ResponseBody
    public Map<String, Object> login(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        String loginName = request.getParameter("loginName");
        String password = request.getParameter("password");
        String clientId = request.getParameter("clientId");
        VSysUsers vUser = this.loginService.findVuser(loginName);
        if (vUser == null) {
            map.put("msg", LoginResult.LOGINNAMENOTFOUND.getContext());
            map.put("loginStatus", "1");
            return map;
        } else if (vUser.getIsLock().equals("1")) {
            //用户状态为锁定
            map.put("msg", LoginResult.LOGINUSERISLOCKED.getContext());
            map.put("loginStatus", "1");
            return map;
        } 
        //校验账户期限
        try {
            if(!this.loginService.getAccountValidity(loginName)){
                map.put("msg", LoginResult.ACCOUNTEXPIRED.getContext());
                map.put("loginStatus", "1");
                return map;
            }
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        String md5Pass = vUser.getPass();
        String pass = MD5Util.getDigest(password);
        if (!pass.equals(md5Pass)) {
            map.put("msg", LoginResult.ERRORPASSWORD.getContext());
            map.put("loginStatus", "1");
            return map;
        }
        map.put("msg", LoginResult.LOGINOK.getContext());
        try {
            messageService.bindAppClientId(loginName, clientId);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        //登录成功标志
        map.put("loginStatus", "0");
        //账户类型 1,pack厂商账户，2,客户
        map.put("accountType", vUser.getAccounttype());
        map.put("company", vUser.getOwnerComp());
        map.put("username", vUser.getEmpName());
        if("1".equals(vUser.getAccounttype())){
            map.put("company", "力神（青岛）新能源有限公司");
        }
        return map;
    }

}
